Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

172
Vistas
Value is defined but still showing "cannot read the properties of null" (foreach)

I was working on a Js project that takes notes through a textarea using a button with a click event. I have stored the value from the text area in localstorage in "notes" key. So all i did was that i parsed it pushed the value from txtarea and then stringified it and further parsed it for an html change but now when i apply a forach loop on the PARSED form of notes it shows error - cannot read the porps of null reading foreach.

console.log("Welcome To MagicNotes");

// if users adds a note add it to the local storage
let addbtn = document.getElementById("addbtn");
// let notes = document.getElementById('notes')

// add a eventlistener -- click
addbtn.addEventListener("click", function (e) {

  //firstly we get/define the values
  let addtxt = document.getElementById("addtxt"); //get the textarea by its id
  let notes = localStorage.getItem("notes"); //get a notes array in the local storage

  // here comes the operation part -->

  // do nothing if value is null
  if (notes == null) {
    notesObj = [];

    // but parse if finds a string
  } else {
    notesObj = JSON.parse(notes); //use notesObj to store obj values of "notes"
     //JSON.parse is used to convert a text to object
  }
  // now update the value in that obj
  notesObj.push(addtxt.value);

  localStorage.setItem("notes", JSON.stringify(notesObj)); //now stringify notesObj and use it in local storage
  addtxt.value = ""; //to clear the txtarea after adding a note
  console.log(notesObj);
  showNotes();
  
});

function showNotes() {
  let notes = localStorage.getItem("notes");
  if (notes = null) {
    notesObj = [];
  } else {
    notesObj = JSON.parse(notes);
  }
  let html = "";

  notesObj.forEach (function (element, index) {
    html += `
   <div id="notes" class="noteCard my-2 mx-2 card">
   <div class="container my-1">
    <div class="card" style="width: 100%;">
        <div class="card-body">
          <h5 class="card-title">Note ${index + 1}</h5>
          <p class="card-text">${element}</p>
          <button href="#" class="btn btn-warning">Delete</button>
        </div>
      </div>
   </div>
   `;
  });
  let notesElm = document.getElementById("notes");
  if (notes.length == 0) {
    notesElm.innerHTML = html;
  }
}
about 4 years ago · Juan Pablo Isaza
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda